<?php
// 设置时区
date_default_timezone_set("PRC");
include 'MMysql.php';
require_once 'errorhandle.php';
$set_password = '123456';
  

session_start();
$mysql = new MMysql();
$mobile = $_POST['mobile'];
$realname = $_POST['realname'];
$password = $_POST['password'];
if(!empty($_SESSION['uid']) && $_SESSION['uid'] > 0){
    die(json_encode(['code'=>0,'msg'=>'您已经登陆了！']));
}
if($password != $set_password){
    die(json_encode(['code'=>0,'msg'=>'密码错误！']));
}
$user = $mysql->where(['mobile'=>$mobile])->limit(1)->select('user3');
$uid = 0;
if(empty($user)){
    if(empty($_SESSION['openid'])){
        die(json_encode(['code'=>0,'msg'=>'请使用微信客服端打开！']));
    }
    $wx_user = $mysql->where(['openid'=>'\''.$_SESSION['openid'].'\''])->limit(1)->select('user3');
    if(empty($wx_user)){
        die(json_encode(['code'=>0,'msg'=>'请刷新页面！']));
    }
    $update_user['mobile'] = $mobile;
    $update_user['realname'] = $realname;
    $update_user['type'] = 2;
    $res = $mysql->where(['openid'=>'\''.$_SESSION['openid'].'\''])->update('user3',$update_user);
    if(!$res){
        die(json_encode(['code'=>0,'msg'=>'系统错误，请稍后再试！']));
    }
    $uid = $wx_user[0]['id'];
}else{
    $uid = $user[0]['id'];
    if($user[0]['realname'] != $realname){
        $update_user['realname'] = $realname;
        $res = $mysql->where(['id'=>$uid])->update('user3',$update_user);
        if(!$res){
            die(json_encode(['code'=>0,'msg'=>'更新姓名失败！']));
        }
    }
}
//登陆成功
$_SESSION['uid'] = $uid;
$_SESSION['user_info'] = ['mobile'=>$mobile,'realname'=>$realname];
die(json_encode(['code'=>1,'msg'=>'登陆成功！']));